package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseSpuImageService;
import com.atguigu.gmall.product.service.BaseSpuSaleAttrService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 卡尼奶
 * @Date 2022/5/18 21:01
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/product")
public class BaseSkuController {

    @Autowired
    private BaseSpuImageService spuImageService;

    @Autowired
    private BaseSpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 根据spuId获取图片列表
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImages = spuImageService.getSpuImageList(spuId);
        return Result.ok(spuImages);
    }

    /**
     * 根据spuId获取销售属性
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSaleAttrService(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> saleAttrs = spuSaleAttrService.getSaleAttrService(spuId);
        return Result.ok(saleAttrs);
    }


    /**
     * 保存sku商品信息
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 获取sku分页列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<SkuInfo> infoPage = new Page<>(page,limit);
        infoPage = skuInfoService.page(infoPage);
        return Result.ok(infoPage);
    }

    /**
     * 上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.onOrcanceSale(skuId,1);
        return Result.ok();
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.onOrcanceSale(skuId,0);
        return Result.ok();
    }
}
